import java.util.Arrays;

class Student{
    public String name;
    public int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

public class StringTest {
    public static void main(String[] args) {
//        StringBuilder stringBuilder = new StringBuilder("hello");
//        String s = stringBuilder.toString();
//        System.out.println(s);
        String s = "hello";
        StringBuilder stringBuilder = new StringBuilder(s);
        stringBuilder.append("abc");
        System.out.println(stringBuilder);
    }
    public static void main12(String[] args) {
        String str = "";
        for (int i = 0; i < 100; i++) {
            str += i;
        }
        System.out.println(str);

        String str2 = "abcd";
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(str2);
        for (int i = 0; i < 100; i++) {
            stringBuilder.append(i);
        }
        System.out.println(stringBuilder);

    }
    public static void main11(String[] args) {
        char[] ch = {'d','i','o'};
        String str1 = new String(ch);
        str1.intern();//把str1指向的对象放到常量池
        //如果常量池已经有了，就不会放入，结果为false
        String str2 = "abc";
        System.out.println(str1==str2);
        //输出为true，去掉intern为false
    }

    public static void main10(String[] args) {
        //包含
        String str1 = "helloWorld";
        boolean ret = str1.contains("hello");//判断是否包含
        //结尾/开头
        String str2 = "abcdef";
        System.out.println(str2.endsWith("def"));//是否以“ ”结尾
        System.out.println(str2.startsWith("abc"));//是否....
    }
    public static void main9(String[] args) {
        //截取
        String str1 = "HelloWorld";
        String ret1 = str1.substring(1);//从1下标截取
        String ret2 = str1.substring(1,4);//[1,4)
        System.out.println(ret1);

        System.out.println("==================");
        String str3 = "  hello hello        ";
        String ret3 = str3.trim();//去掉字符串左右两边的空格
        System.out.println(ret3);
        System.out.println("world");
    }

    public static void main8(String[] args) {
        //字符串拆分
        String str = "德川&我修院";
        String[] ret = str.split("&");
        System.out.println(Arrays.toString(ret));
        System.out.println("===================");
        String str2 = "197.0.0.21";
        String[] ret2 = str2.split("\\.");
        for (String x:ret2) {
            System.out.print(x+" ");
        }

        //斜杠拆分
        System.out.println();
        System.out.println("===================");
        String str3 = "197\\0\\0\\21";
        //String[] ret3 = str3.split("\\\\");
        String[] ret3 = str3.split("/");
        for (String x:ret3) {
            System.out.print(x+" ");
        }
        //单次拆分多个字符
        System.out.println();
        System.out.println("===================");
        String str4 = "name=李田所&age=24";
        String[] ret4 = str4.split("=|&");//以=和&分割
        for (String x:ret4) {
            System.out.print(x+" ");
        }

        //多次拆分单个字符
        System.out.println();
        System.out.println("===================");
        String str5 ="name=zhangsan&age=18";
        String[] ret6 = str5.split("&");
        for (String x : ret6) {
            String[] ss = x.split("=");
            for (String s : ss) {
                System.out.println(s);
            }
        }
    }


    public static void main7(String[] args) {
        //字符串替换
        String str1 = "abcabcdabcde";
        String ret1 = str1.replace('a','p');//把a换成p
        String ret2 = str1.replace("ab","pc");
        String ret3 = str1.replaceAll("abc","6");
        String ret4 = str1.replaceFirst("ab","66");//把第一个ab替换
    }
    public static void main6(String[] args) {
        //转换
        String str = "hello";
        String ret = str.toUpperCase();//转大写
        System.out.println(ret);

        String str2 = "WORLD";
        String ret2 = str2.toLowerCase();//转小写

        System.out.println("================");
        char chars[] = str2.toCharArray();//转变成数组
        System.out.println(chars);

        String str4 = String.format("%d %d %d",2022, 12, 7);
        System.out.println(str4);
    }
        //各种形式转换
    public static void main5(String[] args) {
        String str = String.valueOf(123);
        System.out.println(str);
        String str2 = String.valueOf(true);
        System.out.println(str2);
        String s3 = String.valueOf(new Student("李田所",24));
        System.out.println(s3);

        System.out.println("===================");
        int val1 = Integer.parseInt("123");//将字符串转化为整数123
        int val3 = Integer.valueOf("123");//和val1一样
        System.out.println(val1+1);
        double val2 = Double.parseDouble("12.25");
        System.out.println(val2);
    }


    public static void main4(String[] args) {
        String str = "qqqabcdef";
        //遍历字符串
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            System.out.print(ch );
        }
        //找到某个字符的下标
        System.out.println(str.indexOf('c'));//从头开始找下标
        System.out.println(str.indexOf('c',3));//从下标为3处找
        System.out.println(str.indexOf("abc"));//找字符串（）返回a的下标
        System.out.println("=====================");
        System.out.println(str.lastIndexOf('c'));//从后往前找
        System.out.println(str.lastIndexOf('c',3));
    }
    public static void main3(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("world");
        String s3 = new String("Hello");
        int ret = s1.compareTo(s2);
        int ret2 = s1.compareToIgnoreCase(s3);//忽略大小写比较
        if(ret<0){
            System.out.println("s1<s2");
        } else if (ret == 0) {
            System.out.println("s1==s2");
        }else {
            System.out.println("s1>s2");
        }
    }
    public static void main2(String[] args) {
        String s1 = new String("hello");
        String s2 = new String("hello");
        System.out.println(s1 == s2);//false
        System.out.println(s1.equals(s2));//true
    }
    public static void main1(String[] args) {
        //常用的三种构造字符串方式
        String string1 = "W";

        String string2 = new String("qi");

        char[] chars = {'y','u'};
        String string3 = new String(chars);
    }
}
